<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
    public function __construct()
    {
        parent::__construct();
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->dependentModel = $this->load->model('Dependent_Model');
        $this->dbGeneric = $this->load->model('Dbgeneric_Model');
        $this->educationModel = $this->load->model('Education_Model');
        $this->employmentModel = $this->load->model('Employment_Model');
    }

	public function login($api,$username,$password){
		$login = $this->userModel->login(urldecode($username), $password);
		if(!empty($login)) {
			$response["message"] = "success";
			$response["data"] = array('user_id'=>$login[0]->id,
			'username'=>$login[0]->username,
			'client_id'=>$login[0]->client_id,
			'is_active'=>$login[0]->is_active,
			'role'=>$login[0]->role_name,
			'client_name' => $login[0]->client_name,
			'client_api' => $login[0]->client_api .'-'.$login[0]->client_code
			);
		} else {
			$response["message"] = "error";
			$response["code"] = ErrorConstants::$ERROR_CODE_CREDENTIALS_MISMATCHED;
			
		}
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	//Sample call: http://api.kanding.com/user/register/f2607e0e3e3efb8f-2012/jvicente/simon
	public function register($api, $username, $password, $client_id = 0){
		$client = $this->clientModel->find($api);
		$info = array('username'=>urldecode($username),'password'=>CommonUtils::encryptPassword($password), 
		'client_id'=>$client->id, 'status'=>1 ,'is_active'=>1);
		$id = $this->User_Model->save($info);
		
		if($id){
			$response["message"] = "success";
			$response["data"] = array('user_id'=>$id);
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('user', $data);
	}
	
	public function file201($api){
		$info = $this->input->post();
		$user_id = $this->input->post('user_id');
		//$id = $this->userModel->saveOrUpdateData('file201',$info, $user_id);
		$id = $this->dbGeneric->saveOrUpdate('file201',$info, 'user_id');
		$response["message"] = "success";
		$response["data"] = array('user_id'=>$id);
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);

	}
	
	public function salary($api){
		$info = $this->input->post();
		$return = $this->dbGeneric->saveOrUpdate('salary',$info, 'user_id');
		$this->userModel->saveOrUpdateUserRates($info['user_id']);
		$response["message"] = "success";
		//$response["data"] = array('user_id'=>$this->input->post('user_id'));
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);

	}
	
	public function get_salary($api, $user_id){
		$info = $this->userModel->getUserSalary($api, $user_id);
		$details = $this->dbGeneric->find('user_rates',array('user_id'=>$user_id));
		if(isset($info)) {
			$array['info'] = $info[0];
			$array['details'] = $details[0];
			$response["data"] = $array;
			
		}
		$response["message"] = "success";
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	public function dependent($api){
		if($this->input->post()){
			$params = ObjectUtils::jsonToArray(urldecode($this->input->post('data')));
			$id = $this->dependentModel->saveOrUpdateData('dependents',$params);
			//var_dump();
			$response["message"] = "success";
			$response["data"] = $params;
			$response['id'] = $id;
			//$response["data"] = array('user_id'=>$this->input->post('user_id'));
			$data["response"] = ObjectUtils::arrayToJSON($response);
			$this->load->view('user', $data);
		}

	}
	
	public function education($api){
		if($this->input->post()){
			
			$params = ObjectUtils::jsonToArray(urldecode($this->input->post('data')));
			$id = $this->educationModel->saveOrUpdateData('education',$params);
			$response["message"] = "success";
			$response["data"] = $params;
			$response["id"] = $id;
			$data["response"] = ObjectUtils::arrayToJSON($response);
			$this->load->view('user', $data);
		}
	}
	
	public function employment($api){
		if($this->input->post()){
			
			$params = ObjectUtils::jsonToArray(urldecode($this->input->post('data')));
			//var_dump($params);
			$id = $this->employmentModel->saveOrUpdateData('employment',$params);
			$response["message"] = "success";
			$response["data"] = $params;
			$response["id"] = $id;
			$data["response"] = ObjectUtils::arrayToJSON($response);
			$this->load->view('user', $data);
		}
	}
	
	public function get_dependent($api, $user_id, $id){
		$dependent = $this->dependentModel->find(array('user_id'=>$user_id, 'id'=>$id));
		$response["message"] = "success";
		$response["data"] = $dependent;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function get_education($api, $user_id, $id){
		$dependent = $this->educationModel->find(array('user_id'=>$user_id, 'id'=>$id));
		$response["message"] = "success";
		$response["data"] = $dependent;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function get_employment($api, $user_id, $id){
		$dependent = $this->employmentModel->find(array('user_id'=>$user_id, 'id'=>$id));
		$response["message"] = "success";
		$response["data"] = $dependent;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function delete_dependent($api, $user_id, $id){
		$dependent = $this->dependentModel->delete($api, $user_id, $id);

		$response["message"] = isset($dependent)?"success":"error";
		$response["data"] = array('user_id'=>$user_id,'id'=>$id);
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function delete_education($api, $user_id, $id){
		$dependent = $this->educationModel->delete($api, $user_id, $id);

		$response["message"] = isset($dependent)?"success":"error";
		$response["data"] = array('user_id'=>$user_id,'id'=>$id);
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function delete_employment($api, $user_id, $id){
		$employment = $this->employmentModel->delete($api, $user_id, $id);

		$response["message"] = isset($employment)?"success":"error";
		$response["data"] = array('user_id'=>$user_id,'id'=>$id);
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	public function display_members($api){
		//$settings = $this->config->item('settings');
		$all_users = $this->User_Model->getAllUsers($api);
		$response["message"] = "success";
		$response["data"] = $all_users;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
		//var_dump($all_users);
	}
	
	public function uploadPicture($api){
		if($this->input->post()) {
			$params = $this->input->post();
			$return = $this->User_Model->uploadPicture($api, $params);
			if($return === true){
				$response["message"] = "success";
			}else{
				$response["message"] = "error";
				$response["code"] = ErrorConstants::$ERROR_CODE_TABLE_UPDATE_ERROR;
				
			}
		}else{
			$response["message"] = "error";
			$response["code"] = ErrorConstants::$ERROR_CODE_METHOD_GET_ALLOWED;
		}

		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('user', $data);
	}
	
	
	
	public function getProfile($api,$user_id){
		$data = array();
		$return = $this->userModel->getUserProfile($api, $user_id);
		if($return !== false){
			$response["message"] = "success";
			$response["data"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		
		$this->load->view('user', $data);
		
	}
	
	public function getDependents($api,$user_id){
		$data = array();
		$return = $this->userModel->getDependents($api, $user_id);
		if($return !== false){
			$response["message"] = "success";
			$response["data"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		
		$this->load->view('user', $data);
		
	}
	
	public function getEmployment($api,$user_id){
		$data = array();
		$return = $this->userModel->getEmployment($api, $user_id);
		if($return !== false){
			$response["message"] = "success";
			$response["data"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		
		$this->load->view('user', $data);
	}
	
	public function getUserSalary($api,$user_id){
		$data = array();
		$user_id = 100;
		$return = $this->userModel->getUserSalary($api, $user_id);
		if($return !== false && !empty($return)){
			$response["message"] = "success";
			$response["data"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		
		$this->load->view('user', $data);
	}
	
	public function getAllRates($api,$user_id){
		$data = array();
		$return = $this->userModel->getAllRates($api, $user_id);
		if($return !== false && !empty($return)){
			$response["message"] = "success";
			$response["data"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		
		$this->load->view('user', $data);
	}
	
	public function contributions($api){
		$data = array();
		//$client = $this->clientModel->find($api);
		//$params = ObjectUtils::jsonToArray(urldecode($this->input->post('data')));
		
		if($this->input->post()){
			$params = ObjectUtils::jsonToArray(urldecode($this->input->post('data')));
			$return = $this->userModel->saveContributions($params);
			$response["message"] = "success";
			$response["data"] = $params;
			$response["key"] = $return;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('user', $data);
		
	}
	
	public function get_contributions($api,$user_id){
		$data = array();
		if(is_numeric($user_id)){
			$contributions = $this->userModel->getContributions($api,$user_id);
			$response["message"] = "success";
			$response["data"] = $contributions;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('user', $data);
		
	}

	
}